--[[
多人副本逻辑
--]]
local ExcelMap = require("excel.Map")
local CopyConfig = ExcelMap.copy
local SceneConfig = ExcelMap.scene
local Timer = require("core.Timer")
local ObjHuman = require("core.ObjHuman")
local Copy = require("copy.CopyL")
local Loss = require("copy.Loss")
local CopyHandler = require("copy.HandlerL")
local CopyDefine = require("copy.Define")
local RoleFFI = require("role.RoleFFI")
local Config = require("Config")
local Obj = require("core.Obj")

function applyCopyRoomMulti(copyID, config, uuid)
    if Config.IS_MIDDLE and copyID == CopyDefine.COPY_FAMILYWAR_ID then
        for roomID in pairs(Copy.copyList[copyID]) do
          --跨服军团战，特殊处理
            if Copy.roomDatas[copyID][roomID] == nil then
                Copy.roomDatas[copyID][roomID] = {member = {}, onlineNum=0, startTime=Timer.now}             
                Copy.roomBegin(copyID, roomID)
                return roomID
            end
        end
    end
    for roomID in pairs(Copy.copyList[copyID]) do  --先查找同队人所在的房间
            if Copy.roomDatas[copyID][roomID] and Copy.roomDatas[copyID][roomID].finishCode == nil then
                local members = Copy.roomDatas[copyID][roomID].member
                for i = 1, #members do
                    local h = ObjHuman.onlineUuid[members[i]]
                    if h and h.db.teamUuidLen > 1 then
                       for j = 0, h.db.teamUuidLen-1 do
                          local teamUuid = RoleFFI.ffi_string(h.db.teamUuid[j].uuid)
                          if teamUuid == uuid then
                              members[#members + 1] = uuid
                              Copy.copyHuman2Room[uuid] = roomID
                              return roomID
                          end
                       end
                    end
                end            
            end
    end
    for roomID in pairs(Copy.copyList[copyID]) do
            -- 看是否有未满人的房间
            if Copy.roomDatas[copyID][roomID] and Copy.roomDatas[copyID][roomID].finishCode == nil and #Copy.roomDatas[copyID][roomID].member < config.maxNum then
                local member = Copy.roomDatas[copyID][roomID].member
                member[#member + 1] = uuid
                Copy.copyHuman2Room[uuid] = roomID
                return roomID
            end        
    end

    for roomID in pairs(Copy.copyList[copyID]) do
        -- 全部已开的房间已满人 看是否有未分配的房间
        if Copy.roomDatas[copyID][roomID] == nil then
            Copy.roomDatas[copyID][roomID] = {member = {uuid}, onlineNum=0, startTime=Timer.now}
            Copy.copyHuman2Room[uuid] = roomID
            Copy.roomBegin(copyID, roomID)
            return roomID
        end
    end

    return nil
end

function humanBeginCopy(human, copyID, roomID, x, y, args)
    local copyConfig = CopyConfig[copyID]
    local mapID = nil
    
    if not args or not args.mapID then
        mapID = copyConfig.mapList[1]
    elseif args and args.mapID then
        mapID = args.mapID
    end
    local sceneConfig = SceneConfig[mapID]

    CopyHandler.incCopyEnterCount(human, copyID)

    Loss.handler(human, copyConfig, copyID)	-- 扣除进入需要的消耗

    if Config.IS_MIDDLE and copyID == CopyDefine.COPY_FAMILYWAR_ID then
         local member = Copy.roomDatas[copyID][roomID].member
         member[#member + 1] = human._id
         Copy.copyHuman2Room[human._id] = roomID
    end
    if not x then
        x = sceneConfig.x
    end

    if not y then
        y = sceneConfig.y
    end

    ObjHuman.enterMap(human, mapID, x, y)
    human.enterCopyTime = Timer.now
end

function humanEndCopy(human, args)
    local sceneID = human.scene_id
    local copyID = Copy.sceneID2CopyID[sceneID]
    local roomID = Copy.sceneID2Room[sceneID]
    local logic_id = _G.logic_id

    if not args or not args.copyID then
        local backHomeMapId, backHomeX, backHomeY = Copy.getBackHomePoint(human, copyID)
        ObjHuman.enterMap(human, backHomeMapId, backHomeX, backHomeY)
    elseif args and args.copyID then
        local nAssCopyConfig = CopyConfig[args.copyID]
        if nAssCopyConfig then
            local nAssMapID = nAssCopyConfig.mapList[1]
            local cf = ExcelMap.scene[nAssMapID]
            if Copy.isCopy(nAssMapID) and cf.logic_id ~= logic_id then
                assert(nil)
            end
            Obj.leaveScene(human)
        end
    end

    -- 多人副本的话 把这个玩家从roomdata中干掉
    Copy.copyHuman2Room[human._id] = nil
    local members = Copy.roomDatas[copyID][roomID].member
    for index, uuid in ipairs(members) do
        if uuid == human._id then
            members[index] = members[#members]
            members[#members] = nil
        end
    end
    local config = CopyConfig[copyID]
    if #members == 0 then -- 全部玩家都主动推出了 那么这个房间clear
        if config.noManThenFinish == 1 then
            Copy.roomClear(copyID, roomID)
        end
    end
end
